import type { VbenFormProps, VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { queryPage, setStatus } from './api';
import { ref } from 'vue';
import { z } from '#/adapter/form';
import { useStoreStore } from '#/store';

// 操作id
export const idKey = ref('tagId');
export const handleId = ref('');
export const storeId = ref(useStoreStore().getStoreId());

// 表格表单数据
export const formOptions: VbenFormProps = {
  schema: [
    {
      component: 'Input',
      defaultValue: '',
      fieldName: 'tagName',
      label: '标签名称',
    },
    {
      fieldName: 'state',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '已显示', value: 1 },
          { label: '已隐藏', value: 0 },
        ],
      },
    },
  ],
  wrapperClass: 'grid-cols-5',
};

// 表格数据
export const gridOptions: VxeTableGridOptions = {
  columns: [
    { title: '序号', type: 'seq', width: 80 },
    { title: '标签名称', align: 'left', field: 'tagName' },
    { title: '排序', align: 'center', field: 'seq', width: 80 },
    {
      title: '显示状态',
      field: 'state',
      cellRender: {
        name: 'CellSwitch',
        props: {
          idKey: idKey.value,
          checkedValue: 1,
          changeQuery: setStatus,
        },
      },
      width: 160,
    },
    { title: '创建时间', field: 'createTime', width: 160 },
    { fixed: 'right', slots: { default: 'action' }, title: '操作', width: 160 },
  ],
  minHeight: '100%',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await queryPage({
          page: page.currentPage,
          limit: page.pageSize,
          storeId: storeId.value,
          ...formValues,
        });
      },
      querySuccess: async ({ response }) => {
        console.log(response);
        let list = response?.data?.records || [];
        useStoreStore().setTagList(list);
      },
    },
    response: {
      result: 'data.records',
      total: 'data.total',
      list: 'data.records',
    },
  },
  pagerConfig: {
    enabled: true,
    pageSize: 10,
  },
  toolbarConfig: {
    custom: true,
    export: false,
    import: false,
    refresh: true,
    zoom: true,
  },
};

// 编辑弹窗表单配置
export const drawerFormSchema: VbenFormSchema[] = [
  {
    component: 'Input',
    defaultValue: '',
    fieldName: idKey.value,
    label: '标签id',
    dependencies: {
      show() {
        return false;
      },
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    defaultValue: storeId.value,
    fieldName: 'storeId',
    label: '微馆id',
    dependencies: {
      show() {
        return false;
      },
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入标签名称',
      allowClear: true,
    },
    fieldName: 'tagName',
    label: '标签名称',
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    componentProps: {
      options: [
        {
          label: '显示',
          value: 1,
        },
        {
          label: '隐藏',
          value: 0,
        },
      ],
    },
    fieldName: 'state',
    defaultValue: 1,
    label: '显示状态',
    rules: 'required',
  },
  {
    component: 'InputNumber',
    fieldName: 'seq',
    componentProps: {},
    label: '排序值',
    rules: 'required',
    defaultValue: 1,
  },
];
